{% extends 'layout.html' %}


{% block content %}
    <div class="row">
        <div class="col-md-12">
            <div class="box box-info">
                <div class="box-header with-border">
                    <h3 class="box-title">项目配置</h3>

                    <div class="box-tools pull-right">
                        <button type="button" class="btn btn-box-tool" data-widget="collapse"><i
                                class="fa fa-chevron-up"></i>
                        </button>
                        <div class="btn-group">
                            <button type="button" class="btn btn-box-tool dropdown-toggle" data-toggle="dropdown">
                                <i class="fa fa-wrench"></i></button>
                            <ul class="dropdown-menu" role="menu">
                                <li><a href="#">Action</a></li>
                                <li><a href="#">Another action</a></li>
                                <li><a href="#">Something else here</a></li>
                                <li class="divider"></li>
                                <li><a href="#">Separated link</a></li>
                            </ul>
                        </div>
                        <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i>
                        </button>
                    </div>
                </div>
                <!--/.box-header -->
                <div class="box-body">
                    <div class="table-responsive">
                        <div id="toolbar">
                            <a href="{% url 'project_conf' %}" class="btn btn-sm btn-default">
                                <i class="fa fa-plus"></i> 新建项目
                            </a>
                            <button id="btn_remove" class="btn btn-sm btn-danger">
                                <i class="fa fa-trash-o"></i> 批量删除
                            </button>
                        </div>
                        <table id="table">
                        </table>

                    </div>
                    <!-- /.row -->
                </div>
                <!-- ./box-body -->
                <div class="box-footer">
                    <div class="row">
                    </div>
                    <!-- /.row -->
                </div>
                <!-- /.box-footer -->
            </div>
            <!-- /.box -->
        </div>
        <!-- /.col -->
    </div>
{% endblock %}

{% block self_footer_js %}
    <script>
        var $table = $('#table'),
            $remove = $('#remove'),
            selections = [];

        function initTable() {
            $table.bootstrapTable({
                method: 'get',
                url: '{% url 'project_getlist' %}',
                toolbar: '#toolbar',
                //指定主键列
                idField: 'id',
                sidePagination: "server",
                //height: getHeight(),
                //设置为 true 会有隔行变色效果
                striped: true,
                search: true,
                showRefresh: true,
                //showToggle: true,
                //showPaginationSwitch: true,
                pagination: true,
                pageSize: 8,
                pageList: [10, 25, 50, 100],
                cache: false,
                clickToSelect: true,
                columns: [
                    {
                        field: 'state',
                        checkbox: true,
                        align: 'center'
                    },
                    {
                        field: 'id',
                        title: 'ID',
                        align: 'center',
                        visible: false
                    },
                    {
                        field: 'name',
                        title: '项目名称',
                        sortable: true,
                        align: 'center',
                        events: nameEvents,
                        formatter: nameFormatter
                    }
                    , {
                        field: 'env',
                        title: '环境',
                        align: 'center'
                    }
                    , {
                        field: 'repo_mode',
                        title: '上线方式',
                        align: 'center'
                    }
                    , {
                        field: 'audit',
                        title: '开启审核',
                        align: 'center'
                    }
                    , {
                        field: 'status',
                        title: '状态',
                        align: 'center',
                        formatter: activeFormatter
                    }, {
                        field: 'operate',
                        title: '操作',
                        align: 'center',
                        //events: operateEvents,
                        formatter: operateFormatter
                    }

                ]
            })
        }

        function getHeight() {
            return $(window).height() - $('h1').outerHeight(true);
        }

        function nameFormatter(value, row, index) {
            return [
                '<a class="preview" href="javascript:void(0);">' + value + '</a>'
            ].join('');
        }

        window.nameEvents = {
            'click .preview': function (e, value, row, index) {
                Priview(row.id);
            }
        };

        //preview
        function Priview(id) {
            var addDialog = new BootstrapDialog({
                    title: "项目详细信息",
                    message: function (dialog) {
                        var $message = $('<div></div>');
                        var pageToLoad = dialog.getData('pageToLoad');
                        $message.load(pageToLoad);

                        return $message;
                    },
                    data: {
                        'pageToLoad': '{% url 'project_preview' %}?projectid=' + id
                    },
                })
            ;
            addDialog.open();
        }

        //操作列方法
        function operateFormatter(value, row, index) {
            //console.log(row.id)
            return [
                '<a class="check" href="javascript:void(0)" title="Like">',
                '<i class="fa fa-search"></i> 检测',
                '</a> ',
                '<a class="copy" href="javascript:void(0)" title="Like">',
                '<i class="fa fa-copy"></i> 复制',
                '</a> ',
                '<a class="users" href="javascript:void(0)" title="Like">',
                '<i class="fa fa-users"></i> 成员',
                '</a> ',
                '<a class="edit" href="/publish/project/edit?projectid=' + row.id + '" title="edit">',
                '<i class="fa fa-pencil"></i> 编辑',
                '</a>  ',
                //'<a class="remove red" href="javascript:void(0)" title="Remove">',
                //'<i class="fa fa-trash-o"></i> 删除',
                //'</a>'
            ].join('');
        }
        //状态列
        function activeFormatter(value, row, index) {
            //alert(value);
            s = '';
            if (value == 1) {
                s = [
                    '是'
                ].join('');
            }
            else {
                s = [
                    "<span style='color:red;'>否</span>"
                ].join('');
            }
            return s;
        }

        $(function () {
            //初始化表格
            initTable();

            // delete button
            $("#btn_remove").click(function () {
                var selected = $table.bootstrapTable('getSelections');
                if ($(selected).length > 0) {
                    BootstrapDialog.confirm({
                        title: "警告",
                        type: BootstrapDialog.TYPE_WARNING,
                        message: "确定删除选中的" + $(selected).length + "个项目吗？",
                        btnCancelLabel: '放弃',
                        btnCancelClass: 'btn-white',
                        btnOKLabel: '确认',
                        //btnOKClass: 'btn-default',
                        callback: function (result) {
                            if (result) {
                                var ids = new Array($(selected).length);
                                $.each(selected, function (index, value) {
                                    ids[index] = value.id;
                                });
                                $.post("{% url 'project_del' %}", {ids: ids}, function (data) {
                                        if (data.code != 0) {
                                            $table.bootstrapTable("refresh");
                                            dialogItself.close();
                                        }
                                        else BootstrapDialog.alert(data.message);

                                    }, "json"
                                );
                            }
                        }
                    });
                }
                else BootstrapDialog.warning("请选择要删除的行");
            });
        });
    </script>
{% endblock %}